// 新增图书
// 1. 获取豆瓣信息
// 2. 入库
const https = require('https')
const {mysql} = require('../qcloud')
module.exports = async (ctx) => {
    const {openid, title, summary, image} = ctx.request.body;
    const findRes = await mysql('books').select().where('title',title)
    if (findRes.length) {
        ctx.state = {
            code: -1,
            data: {
                msg: '图书已存在'
            }
        }
        return
    }
    try {
        await mysql('books').insert({
            openid,
            title,
            summary,
            image
        })
        ctx.state = {
            code: "00",
            msg: 'success',
            data: {
                openid,
                title,
                summary,
                image
            }
            
        }
    } catch (e){
        ctx.state = {
            code: -1,
            data: {
                msg: '新增失败:' + e.sqlMessage
            }

        }
    }
    
}